﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Data;
using System.Data.SqlClient;
using System;
using UnityEngine.SceneManagement;

/*
 * 登录
 */
public class RegisterBtn : MonoBehaviour
{
    public Text faied;
    
    public InputField accountInputField;
    
    public InputField passwordInputField;
    private Button btn;
    
    public static string constr = "server=127.0.0.1;database=U3DTest;uid=sa;pwd=1234";
    
    public SqlConnection conn;
    // Start is called before the first frame update
    void Start()
    {
        btn = GetComponent<Button>();
        btn.onClick.AddListener(ButtonClick);
     
        conn = new SqlConnection(constr);
    }
    void ButtonClick()
    {
        conn.Open();
        Debug.Log("链接成功");
       
        string queryStr = "SELECT count(*) FROM dbo.userInfo " +
            "where username='" + accountInputField.text + "' and " +
            "userpassword= '" + passwordInputField.text + "'";
        SqlCommand sqlcommand = new SqlCommand(queryStr, conn);
        object result = sqlcommand.ExecuteScalar();

        int count = Convert.ToInt32(result);
        if (count >= 1)
        {
            Debug.Log("登录成功");
            SceneManager.LoadScene(1);
        }
        else
        {
            faied.gameObject.SetActive(true);
            Debug.Log("登录失败");
        }
        conn.Close();
        Debug.Log("关闭成功");
    }

    // Update is called once per frame
    void Update()
    {

    }
}
